package com.fulong.service.impl;

import com.fulong.dao.DeviceBaiseInfoDao;
import com.fulong.entity.DeviceBaiseInfoEntity;
import com.fulong.service.DeviceBaiseInfoService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fulong.common.utils.PageUtils;
import com.fulong.common.utils.Query;

import com.fulong.dao.DeviceBaiseInfoDao;
import com.fulong.entity.DeviceBaiseInfoEntity;
import com.fulong.service.DeviceBaiseInfoService;


@Service("deviceBaiseInfoService")
public class DeviceBaiseInfoServiceImpl extends ServiceImpl<DeviceBaiseInfoDao, DeviceBaiseInfoEntity> implements DeviceBaiseInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<DeviceBaiseInfoEntity> page = this.page(
                new Query<DeviceBaiseInfoEntity>().getPage(params),
                new QueryWrapper<DeviceBaiseInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<DeviceBaiseInfoEntity> listWithTree() {
        List<DeviceBaiseInfoEntity> deviceBaiseInfoEntities = baseMapper.selectList(null);
        List<DeviceBaiseInfoEntity> collect = deviceBaiseInfoEntities.stream().filter(entiy -> entiy.getLevel() == 0)
                .map((el) -> {
                    el.setChildren(getChildren(el, deviceBaiseInfoEntities));
                    return el;
                })
                .collect(Collectors.toList());
        return collect;
    }
    public List<DeviceBaiseInfoEntity> getChildren(DeviceBaiseInfoEntity parent ,List<DeviceBaiseInfoEntity> all) {
        return all.stream().filter(entiy->entiy.getParent().equals(parent.getId()))
                .map((childrent)->{
                    childrent.setChildren(getChildren(childrent , all));
                    return childrent;
                }).collect(Collectors.toList());
    }

}